﻿using B.S.Base.Read.API.Application.Command;
using B.S.Base.Read.API.Dto;
using MediatR;
using WEBAPI.API.Dto;
using WEBAPI.Damoin;
using WEBAPI.ErroeCode;
using WEBAPI.Indaseture.Infasetures;

namespace B.S.Base.Read.API.Application.CommandHandLer
{
    public class GetApproveCommandHandLer : IRequestHandler<GetApproveCommand, APIResult<APIPaing<List<ApproveModel>>>>
    {
        private readonly IBaseRepostiy<ApproveModel> _approveModel;
        private readonly IBaseRepostiy<UserRoleModel> _userRoleModel;
        private readonly IBaseRepostiy<AccountModel> _accountModel;
        private readonly IBaseRepostiy<RoleModel> _roleModel;
        public GetApproveCommandHandLer(IBaseRepostiy<ApproveModel> approveModel, IBaseRepostiy<UserRoleModel> userRoleModel, IBaseRepostiy<AccountModel> accountModel, IBaseRepostiy<RoleModel> roleModel)
        {
            this._approveModel = approveModel;
            this._userRoleModel = userRoleModel;
            this._accountModel = accountModel;
            _roleModel = roleModel;
        }
        public async Task<APIResult<APIPaing<List<ApproveModel>>>> Handle(GetApproveCommand request, CancellationToken cancellationToken)
        {
            var query = _approveModel.GetAll().Where(x => !x.IsDel);
            string? RoleName = null;
            int? currentUserDeptId = null;
            if (!string.IsNullOrEmpty(request.CurrentUser))
            {
                var account = _accountModel.GetAll().FirstOrDefault(a => a.AccountName == request.CurrentUser);
                if (account != null)
                {
                    currentUserDeptId = account.DeptId;
                    var userRole = _userRoleModel.GetAll().FirstOrDefault(r => r.AccountId == account.AccountId);
                    if (userRole != null)
                    {
                        var role=_roleModel.GetAll().FirstOrDefault(x=>x.RoleId==userRole.RoleId);
                        {
                            if(role!= null)
                            {
                                RoleName=role.RoleName;
                            }
                        }
                    }
                }
            }
            if (RoleName == "董事长")
            {

            }
            else if (RoleName == "总经理" && currentUserDeptId.HasValue)
            {
                var deptAccounts = _accountModel.GetAll().Where(a => a.DeptId == currentUserDeptId.Value).Select(a => a.AccountName).ToList();
                query = query.Where(x => deptAccounts.Contains(x.AccountName));
            }
            else
            {
                if (!string.IsNullOrEmpty(request.CurrentUser))
                    query = query.Where(x => x.AccountName == request.CurrentUser);
            }
            if (!string.IsNullOrEmpty(request.AccountName))
                query = query.Where(x => x.AccountName.Contains(request.AccountName));

            if (!string.IsNullOrEmpty(request.Statu))
                query = query.Where(x => x.Statu == request.Statu);

            if (request.StartTime.HasValue)
                query = query.Where(x => x.StartTime >= request.StartTime.Value.Date);

            if (request.EndTime.HasValue)
                query = query.Where(x => x.EndTime.Date <= request.EndTime.Value.Date);

            int totalCount = query.Count();
            int totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            var list = query.OrderByDescending(x => x.Id)
                            .Skip((request.PageIndex - 1) * request.PageSize)
                            .Take(request.PageSize)
                            .ToList();
            return new APIResult<APIPaing<List<ApproveModel>>>
            {
                Code = ResultCode.Ok,
                Data = new APIPaing<List<ApproveModel>>
                {
                    Data = list,
                    totalCount = totalCount,
                    totalPage = totalPage,
                },
                Message = "获取成功"
            };
        }
    }
}
